import { CrudOptions, AddReq, DelReq, EditReq, dict, CrudExpose, UserPageQuery, CreateCrudOptionsRet, ValueResolveContext, ValueBuilderContext  } from '@fast-crud/fast-crud';
import _ from 'lodash-es';
import * as api from './api';
import { request } from '/@/utils/service';
import { auth } from '/@/utils/authFunction';


//此处为crudOptions配置
export default function ({ crudExpose }: { crudExpose: CrudExpose }): CreateCrudOptionsRet {
	const pageRequest = async (query: any) => {
		return await api.GetList(query);
	};
	const editRequest = async ({ form, row }: EditReq) => {
		if (row.id) {
			form.id = row.id;
		}
		return await api.UpdateObj(form);
	};
	const delRequest = async ({ row }: DelReq) => {
		return await api.DelObj(row.id);
	};
	const addRequest = async ({ form }: AddReq) => {
		return await api.AddObj(form);
	};

	const exportRequest = async (query: UserPageQuery) => {
		return await api.exportData(query);
	};

	return {
		crudOptions: {
			request: {
				pageRequest,
				addRequest,
				editRequest,
				delRequest,
			},
			actionbar: {
				buttons: {
					export: {
						// 注释编号:django-vue3-admin-crud210716:注意这个auth里面的值，最好是使用index.vue文件里面的name值并加上请求动作的单词
						show: auth('SaleDataViewSet:Export'),
						text: '导出', //按钮文字
						title: '导出', //鼠标停留显示的信息
						click() {
							return exportRequest(crudExpose.getSearchFormData());
							// return exportRequest(crudExpose!.getSearchFormData())    // 注意这个crudExpose!.getSearchFormData()，一些低版本的环境是需要添加!的
						},
					},
					add: {
						show: auth('SaleDataViewSet:Create'),
						text: '新增', //按钮文字
						title: '新增', //鼠标停留显示的信息
					},
				},
			},
			rowHandle: {
				//固定右侧
				fixed: 'right',
				width: 300,
				buttons: {
					view: {
						type: '',
						order: 1,
						show: auth('SaleDataViewSet:Retrieve'),
					},
					edit: {
						type: 'success',
						order: 2,
						show: auth('SaleDataViewSet:Update'),
					},
					copy: {
						type: 'primary',
						order: 3,
						show: auth('SaleDataViewSet:Copy'),
					},
					remove: {
						type: 'danger',
						order: 4,
						show: auth('SaleDataViewSet:Delete'),
					},
				},
			},
			columns: {
				name: {
					title: '项目名称',
					type: 'input',
					search: { show: true },
					column: {
						minWidth: 120,
						sortable: 'custom',
					},
					form: {
						rules: [{ required: true, message: '项目名称必填' }],
						component: {
							placeholder: '请输入项目名称',
						},
					},
				},
				field: {
					title: '关联key',
					type: 'input',
					search: { show: true },
					column: {
						minWidth: 120,
						sortable: 'custom',
					},
					form: {
						rules: [{ required: true, message: '项目名称必填' }],
						component: {
							placeholder: '请输入项目名称',
						},
					},
				},
				success_judgment: {
					title: '成功判定',
					type: 'dict-radio',
					search: { show: true },
					column: {
						minWidth: 120,
						sortable: 'custom',
					},
					defaultValue: 0,
					dict: dict({
						//本地数据字典
						value: 'key',
						label: 'text',
						data: [
							{ key: 0, text: '小于等于' },
							{ key: 1, text: '大于等于' },
						],
					}),
					form: {
						rules: [{ required: true, message: '成功判定必填' }],
						component: {
							placeholder: '请选择成功判定',
						},
					},
				},
				show_progress: {
					title: '显示进度',
					type: 'dict-radio',
					search: { show: true },
					column: {
						minWidth: 120,
						sortable: 'custom',
					},
					dict: dict({
						//本地数据字典
						value: 'key',
						label: 'text',
						data: [
							{ key: false, text: '不显示'},
							{ key: true, text: '显示' },
						],
					}),
					form: {
						rules: [{ required: true, message: '显示进度必填' }],
						component: {
							placeholder: '请选择显示进度',
						},
					},
				},
				modifier_name: {
					title: '最后修改人',
					type: 'text',
					search: { show: false },
					column: {
						width: 200,
						sortable: 'custom',
					},
					form: {
						show: false,
					},
				},
				creator_name: {
					title: '创建人',
					type: 'text',
					search: { show: false },
					column: {
						width: 200,
						sortable: 'custom',
					},
					form: {
						show: false,
					},
				},
			},
		},
	};
}
